package com.ytq.share.service.impl;

import com.ytq.share.dto.CustomUserDTO;
import com.ytq.share.entity.User;
import com.ytq.share.mapper.UserMapper;
import com.ytq.share.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.List;
import java.util.Random;

/**
 * @author yuantongqin
 * description:
 * 2020/12/7
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;


    /**
     * ShardingSphereTransactionType 表示分布式事务 这个是5.1版本的注解
     * ShardingTransactionType 这个是5.0及以下版本的注解
     * @param userPO
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
//    @ShardingSphereTransactionType(TransactionType.XA)
    public void saveUser(User userPO) {
        userMapper.insert(userPO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveUser1(User userPO) {
        userMapper.insert(userPO);
        int j = 10/0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveListUser(User u) {
        for (int i = 0; i < 10; i++) {
            User userPO = new User();
            int j = new Random().nextInt(100);
            userPO.setUserId((long) j);
            userPO.setUserName("zhang"+j);

            int y = new Random().nextInt(2);
            LocalDate now = LocalDate.now();
            int year = now.plusYears(y).getYear();
            userPO.setYear_num((long) year);
            if(i >6){
                int c = 10/0;
            }
            userMapper.insert(userPO);
        }

    }

    @Override
    public List<User> getUser(Long id) {
        return userMapper.getUserByUserId(id);
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public List<User> getUserListByName(String name) {
        List<User> userListByName = userMapper.getUserListByName(name);
        userListByName.forEach(info->{
//            insertCourseList(info);
            String[] persons = {"zhangfei","guanyu","chaoyu","machao","huangzhong"};
//            List<UserItemPO> collect = IntStream.range(1, 5).mapToObj(i -> {
//                UserItemPO itemPO = new UserItemPO();
//                itemPO.setUserId(info.getUserId());
//                itemPO.setUserItemName(persons[new Random().nextInt(persons.length)]+""+i);
//                return itemPO;
//            }).collect(Collectors.toList());
//            userItemService.batchInsert(collect);
        });
        return userListByName;
    }

    private void insertCourseList(User info) {
        String[] courses = {"Chinese","Math","English","chemistry"};
//        List<UserCoursePO> collect = Arrays.stream(courses).map(item -> {
//            UserCoursePO coursePO = new UserCoursePO();
//            coursePO.setUserId(info.getUserId());
//            coursePO.setCourseName(item);
//            return coursePO;
//        }).collect(Collectors.toList());
//        userCourseService.batchInsert(collect);
    }

    @Override
    public List<CustomUserDTO> getAllUser() {
        return userMapper.getAllUser();
    }
}
